/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qsdm.es.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.qsdm.es.dao.exceptions.IllegalOrphanException;
import com.qsdm.es.dao.exceptions.NonexistentEntityException;
import com.qsdm.es.dao.exceptions.PreexistingEntityException;
import com.qsdm.es.persistence.Nivelformacion;

/**
 *
 * @author alberto
 */
public class NivelformacionJpaController extends BasicJpaController {

    public void create(Nivelformacion Nivelformacion,boolean opentx) throws PreexistingEntityException, Exception {
         
        EntityManager em = null;
        try {
            em = getEntityManager();
            if(opentx)em.getTransaction().begin();
            
            
            em.persist(Nivelformacion);
            
            if(opentx)em.getTransaction().commit();
        } catch (Exception ex) {
        	ex.printStackTrace();
            if (findNivelformacion(Nivelformacion.getId()) != null) {
                throw new PreexistingEntityException("Nivelformacion " + Nivelformacion + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Nivelformacion Nivelformacion) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            
            Nivelformacion persistentNivelformacion = em.find(Nivelformacion.class, Nivelformacion.getId());
           
           
            Nivelformacion = em.merge(Nivelformacion);
          
            
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = Nivelformacion.getId();
                if (findNivelformacion(id) == null) {
                    throw new NonexistentEntityException("The Nivelformacion with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            
            Nivelformacion Nivelformacion;
            try {
                Nivelformacion = em.getReference(Nivelformacion.class, id);
                Nivelformacion.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The Nivelformacion with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
          
            em.remove(Nivelformacion);
            
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Nivelformacion> findNivelformacionEntities() {
        return findNivelformacionEntities(true, -1, -1);
    }

    public List<Nivelformacion> findNivelformacionEntities(int maxResults, int firstResult) {
        return findNivelformacionEntities(false, maxResults, firstResult);
    }

    private List<Nivelformacion> findNivelformacionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Nivelformacion.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Nivelformacion findNivelformacion(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Nivelformacion.class, id);
        } finally {
            em.close();
        }
    }

    public int getNivelformacionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Nivelformacion> rt = cq.from(Nivelformacion.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
